package com.lfk.three.model;

import android.os.Handler;
import android.os.Message;

import com.lfk.three.con.JsonHandler;
import com.lfk.three.con.NetWorkHandler;
import com.lfk.three.item.Ad_Item;
import com.lfk.three.static_info.static_info;
import com.lfk.three.static_info.url_router;
import com.lfk.three.utils.logger.Logger;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by liufengkai on 16/4/9.
 */
public class GetAdImp extends BaseImp {
    private static GetAdImp mInstance = null;

    @Override
    public void init(int temp, Handler handler) {
        super.init(temp, handler);
    }


    public static GetAdImp getInstance() {
        if (mInstance == null) {
            synchronized (GetAdImp.class) {
                if (mInstance == null)
                    mInstance = new GetAdImp();
            }
        }
        return mInstance;
    }

    private ArrayList<Ad_Item> ad_items = new ArrayList<>();

    private StringCallback callback = new StringCallback() {
        @Override
        public void onError(Call call, Exception e) {
            Logger.e("err: get ad page");
        }

        @Override
        public void onResponse(String response) {
            ArrayList<Ad_Item> temp = JsonHandler.handleAdList(response);
            ad_items.addAll(temp);

            Message msg = Imp_Handler.obtainMessage();
            msg.obj = temp;
            msg.what = Imp_Temp;
            Imp_Handler.sendMessage(msg);
        }
    };

    public void refreshAd() {
        Map<String, String> params = new Hashtable<>();
        params.put("username", static_info.user_name);
        params.put("usertoken", static_info.user_token);
        NetWorkHandler.Get(url_router.GET_AD_PIC, callback, params);
    }
}
